<?php

/**
 *	@name		商品拍卖
 *	@author		Jeffery
 *	@qq			1017756891
 *	@email		1017756891@qq.com
 *	@website	http://www.iweitao.net
**/

require_once "core.php";

//查询当前最高出价信息
 $query = DB::query("SELECT a.tid,a.gid,a.username,a.price,a.time,b.title FROM `".DB::table("paipai_buy")."` as a inner join `".DB::table('paipai_goods')."` as b on a.gid=b.gid where a.status=1 order by a.time desc limit 1");
 $newest = db_get_one($query);
 



/*************获取分页参数****************/
if($core->_get("page")) {
	$page = $core->_get("page");	
	$pagecount = $count;			//每页显示数
	$page = $page - 1;				
	$mulipage = $page*$count;		//当前开始记录
	$page = $page + 1;				//当前页
} else {
	$mulipage = 0;
	$page = 1;						//当前页
	$pagecount = $page*$count;
}

// 设置分页limit
$limit = ' limit '.$mulipage.','.$pagecount;
$getcount = 0;
/*****************************/
// 获得所有拍卖分类
/*****************************/
$query = DB::query("SELECT made FROM `".DB::table("paipai_goods")."` group by made order by gid");

$re =db_get_array($query);
// var_dump($re);
$mades = array();
if(is_array($re)){
	foreach($re as $v){
		$mades[urlencode($v['made'])] = $v['made'];
	}
}


/*****************************/
// 查看查询条件
/*****************************/
/***************条件开始*****************/
if(isset($_GET['view'])){
	switch ($_GET['view']) {
		case 'my_bid':
			// 查询我出价的商品
			if(!$_G['uid']){
				showmessage('请登录查看');
			}
			$query = DB::query("SELECT count(*) FROM `".DB::table('paipai_goods')."` AS g left JOIN `".DB::table('paipai_buy')."` AS b ON g.gid=b.gid  group by g.gid");
			// 总数
			$getcount = db_get_one($query);
			$getcount = $getcount['count(*)'];

			$query = DB::query("SELECT distinct * FROM `".DB::table('paipai_goods')."` AS g left JOIN `".DB::table('paipai_buy')."` AS b ON g.gid=b.gid group by g.gid".$limit);
			$array = db_get_array($query);
			// var_dump($array);
			break;
		case 'my_attention':
			// 查询我关注的商品
			if(!$_G['uid']){
				showmessage('请登录查看');
			}
			$query = DB::query("SELECT count(*) FROM `".DB::table('paipai_goods')."` AS g INNER JOIN `".DB::table('paipai_attention')."` AS b ON g.gid=b.gid where b.uid=".$_G['uid']);
			// 总数
			$getcount = db_get_one($query);
			$getcount = $getcount['count(*)'];

			$query = DB::query("SELECT * FROM `".DB::table('paipai_goods')."` AS g INNER JOIN `".DB::table('paipai_attention')."` AS b ON g.gid=b.gid where b.uid=".$_G['uid'].$limit);
			$array = db_get_array($query);

			break;
		case 'show_history':
			// 查询历史拍卖商品
			$where = 'where status>1';
			$query = DB::query("SELECT count(*) FROM `".DB::table("paipai_goods")."` ".$where." ORDER BY `pub_time`  DESC");
			// 总数
			$getcount = db_get_one($query);
			$getcount = $getcount['count(*)'];

			$query = DB::query("SELECT * FROM `".DB::table("paipai_goods")."` ".$where." ORDER BY `pub_time`  DESC".$limit);
			$array = db_get_array($query);

			break;
		default:
			# code...
			break;
	}
}else{
	/********************************/
	// 正常显示和分类查询
	/********************************/
	// 查询条件
	$where = ' where 1 ';
	// var_dump($_GET);
	//显示的分类
	if(isset($_GET['made'])){
		$where .= ' and made=\''.urldecode($core->_get('made')) .'\'';
	}
	$query = DB::query("SELECT count(*) FROM `".DB::table("paipai_goods")."` ".$where." ORDER BY `pub_time`  DESC");
	// 总数
	$getcount = db_get_one($query);
	$getcount = $getcount['count(*)'];
	// 以下获得拍卖品信息
	$query = DB::query("SELECT * FROM `".DB::table("paipai_goods")."` ".$where." ORDER BY `pub_time`  DESC".$limit);
	$array = db_get_array($query);

	// var_dump($array);die;


}
/***************条件结束*****************/


/*******************************/
// 设置交易时间提示 交易状态提示
/*******************************/
if(is_array($array)){
	$time = time();
	foreach($array as $k=>$v){
		if($v['date_end'] < $time){
			// 设置交易状态 
			// status
			// 1 交易结束，等待结算 
			// 2 流拍  
			// 3 交易结算，等待完成 
			// 4 交易完成
			switch ($v['status']) {
				case '1':
					$str = '交易结束，等待结算';
					break;
				case '2':
					$str = '交易流拍';
					break;
				case '3':
					$str = '交易结算，等待完成';
					break;
				case '4':
					$str = '交易完成';
					break;
			}
			$array[$k]['str'] = $str;
		}elseif($v['date_start'] > $time){//交易开始时间大于当前时间
			// $left_time = get_left_time($v['date_start'] - $time,0);//获得剩余时间字符串表示,不显示秒数
			$array[$k]['str'] = '距离交易开始还有<span class="show_time" start="'.$time.'" end="'.$v['date_start'].'"></span>';
		}elseif($v['date_end'] > $time){//拍卖进行中
			// $left_time = get_left_time($v['date_end'] - $time);
			$array[$k]['str'] = '拍卖进行中，剩余时间:<span class="show_time" start="'.$time.'" end="'.$v['date_end'].'"></span>';

		}
		

	}
}

/*******************************/
//登录用户查找已关注
/*******************************/
if($_G['uid']>0){
	$atten_query = DB::query("SELECT * FROM `".DB::table("paipai_attention")."` where uid='".$_G['uid']."'");
	$atten_query = db_get_array($atten_query);
	// 默认为为关注
	foreach ($array as $key=>$value) {
		$array[$key]['attention'] = 0;
	}
	// var_dump($atten_query);die;
	if ($array && $atten_query) {
		foreach ($atten_query as $v) {
			$gid = $v['gid'];
			foreach ($array as $key=>$value) {
				if($value['gid'] == $gid){
					$array[$key]['attention'] = 1;
				}
			}
		}
	}
}else{
	foreach ($array as $key=>$value) {
		$array[$key]['attention'] = 0;
	}
}


/**********************分页*************************/
// 获得并设置url
$url = $_SERVER['REQUEST_URI'];
$uarr = parse_url($url);
parse_str($uarr['query'],$ustr);
if(isset($ustr['page']))  unset($ustr['page']);
$url = $uarr['path'] . '?' . http_build_query($ustr);

$page_show = multi($getcount, $count, $page, $url, $maxpages = 0, $page = 10, $autogoto = FALSE, $simple = FALSE);



// echo '<pre>';
// print_r($array);
// exit;
$navtitle = "竞拍";
include template("iweitao_paipai:index");
?>